package com.jwds.digital.bios.business.service.anomaly.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.jwds.digital.bios.business.core.infrastructure.entity.belt.BeltAnomalyInfo;
import com.jwds.digital.bios.business.core.infrastructure.entity.belt.BeltHealthConfig;
import com.jwds.digital.bios.business.core.infrastructure.mapper.belt.BeltAnomalyInfoMapper;
import com.jwds.digital.bios.business.service.anomaly.ConfigVerify;
import com.jwds.digital.bios.business.service.health.HealthScoreDB;
import com.jwds.digital.bios.business.service.anomaly.AnomalyVerifyDB;
import com.jwds.digital.bios.business.service.anomaly.AnomalyVerifyInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Component
public abstract class AbstractAnomalyVerify implements AnomalyVerifyInterface {
    @Autowired
    private BeltAnomalyInfoMapper beltAnomalyInfoMapper;

    @Override
    public void verify(Long beltId) {
        //获取负载率的评分配置项
        List<BeltHealthConfig> config = HealthScoreDB.getBeltHealthConfig(getDeductionItem());
        if (CollectionUtil.isEmpty(config)) {
            return;
        }
        BigDecimal verifyValue = getVerifyValue(beltId);
        //根据评分项配置判断是否异常
        for (BeltHealthConfig conf : config) {
            //判断是否异常
            if (ConfigVerify.verify(conf, verifyValue)) {
                //异常处理：添加异常信息
                BeltAnomalyInfo beltAnomalyInfo = new BeltAnomalyInfo();
                beltAnomalyInfo.setAnomalyItem(conf.getDeductionItem());
                beltAnomalyInfo.setBeltId(beltId);
                beltAnomalyInfo.setAnomalyTime(LocalDateTime.now());
                beltAnomalyInfo.setSuggestion(conf.getDeductionSuggestion());
                beltAnomalyInfo.setRouteId(AnomalyVerifyDB.getRouteId(beltId));
                beltAnomalyInfo.setTaskId(AnomalyVerifyDB.getTaskId(beltId));
                beltAnomalyInfoMapper.insert(beltAnomalyInfo);
                return;
            }
        }
    }

    /**
     * 获取需要校验的值
     * @param beltId
     * @return
     */
    abstract public BigDecimal getVerifyValue(Long beltId);
}
